package net.sx.minemodule.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.sx.minemodule.R;

/**
 * 实现我的订单 五个tab的布局与逻辑
 *
 * @author 11470
 */
public class MineOrderTabView extends LinearLayout {

    private View view;

    private RelativeLayout rlnAllMineOrder;
    private RelativeLayout rlnTwoOrder;
    private RelativeLayout rlnThreeOrder;
    private RelativeLayout rlnFourOrder;
    private RelativeLayout rlnFiveOrder;
    private TextView tvContentAll;
    private TextView tvTwoOrder;
    private TextView tvThreeOrder;
    private TextView tvFourOrder;
    private TextView tvFiveOrder;
    private View viewAllOrder;
    private View viewTwoOrder;
    private View viewThreeOrder;
    private View viewFourOrder;
    private View viewFiveOrder;
    
    private int selectedColorId = R.color.color_a541c9;
    private int noSelectColorId = R.color.color_333333;

    private OneTabClickListener oneTabClickListener;
    private TwoTabClickListener twoTabClickListener;
    private ThreeTabClickListener threeTabClickListener;
    private FourTabClickListener fourTabClickListener;
    private FiveTabClickListener fiveTabClickListener;
    private ViewPager viewPager;

    public MineOrderTabView(Context context) {
        this(context, null);
    }

    public MineOrderTabView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MineOrderTabView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        view = LayoutInflater.from(context).inflate(R.layout.mine_view_mine_order_tab, this, true);
        rlnAllMineOrder = view.findViewById(R.id.rln_all_mine_order);
        tvContentAll = view.findViewById(R.id.tv_content_all);
        viewAllOrder = view.findViewById(R.id.view_all_order);
        rlnTwoOrder = view.findViewById(R.id.rln_two_order);
        tvTwoOrder = view.findViewById(R.id.tv_two_order);
        viewTwoOrder = view.findViewById(R.id.view_two_order);
        rlnThreeOrder = view.findViewById(R.id.rln_three_order);
        tvThreeOrder = view.findViewById(R.id.tv_three_order);
        viewThreeOrder = view.findViewById(R.id.view_three_order);
        rlnFourOrder = view.findViewById(R.id.rln_four_order);
        tvFourOrder = view.findViewById(R.id.tv_four_order);
        viewFourOrder = view.findViewById(R.id.view_four_order);
        rlnFiveOrder = view.findViewById(R.id.rln_five_order);
        tvFiveOrder = view.findViewById(R.id.tv_five_order);
        viewFiveOrder = view.findViewById(R.id.view_five_order);

        tvContentAll.setTextColor(getResources().getColor(selectedColorId));
        viewAllOrder.setBackgroundColor(getResources().getColor(selectedColorId));
        tvTwoOrder.setTextColor(getResources().getColor(noSelectColorId));
        viewTwoOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
        tvThreeOrder.setTextColor(getResources().getColor(noSelectColorId));
        tvFourOrder.setTextColor(getResources().getColor(noSelectColorId));
        tvFiveOrder.setTextColor(getResources().getColor(noSelectColorId));
        viewThreeOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
        viewFourOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
        viewFiveOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
        initEvent();
    }

    private void initEvent(){
        rlnAllMineOrder.setOnClickListener(v -> {
            tvContentAll.setTextColor(getResources().getColor(selectedColorId));
            tvTwoOrder.setTextColor(getResources().getColor(noSelectColorId));
            tvThreeOrder.setTextColor(getResources().getColor(noSelectColorId));
            tvFourOrder.setTextColor(getResources().getColor(noSelectColorId));
            tvFiveOrder.setTextColor(getResources().getColor(noSelectColorId));
            viewAllOrder.setBackgroundColor(getResources().getColor(selectedColorId));
            viewTwoOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewThreeOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewFourOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewFiveOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewAllOrder.setVisibility(VISIBLE);
            viewTwoOrder.setVisibility(INVISIBLE);
            viewThreeOrder.setVisibility(INVISIBLE);
            viewFourOrder.setVisibility(INVISIBLE);
            viewFiveOrder.setVisibility(INVISIBLE);
            if (viewPager!=null){
                viewPager.setCurrentItem(0);
            }
            if (oneTabClickListener!=null){
                oneTabClickListener.onOneTabClick(v);
            }
        });
        
        rlnTwoOrder.setOnClickListener(v -> {
            tvContentAll.setTextColor(getResources().getColor(noSelectColorId));
            tvTwoOrder.setTextColor(getResources().getColor(selectedColorId));
            tvThreeOrder.setTextColor(getResources().getColor(noSelectColorId));
            tvFourOrder.setTextColor(getResources().getColor(noSelectColorId));
            tvFiveOrder.setTextColor(getResources().getColor(noSelectColorId));
            viewAllOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewTwoOrder.setBackgroundColor(getResources().getColor(selectedColorId));
            viewThreeOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewFourOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewFiveOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewAllOrder.setVisibility(INVISIBLE);
            viewTwoOrder.setVisibility(VISIBLE);
            viewThreeOrder.setVisibility(INVISIBLE);
            viewFourOrder.setVisibility(INVISIBLE);
            viewFiveOrder.setVisibility(INVISIBLE);
            if (viewPager!=null){
                viewPager.setCurrentItem(1);
            }
            if (twoTabClickListener!=null){
                twoTabClickListener.onTwoTabClick(v);
            }
        });
        
        rlnThreeOrder.setOnClickListener(v -> {
            tvContentAll.setTextColor(getResources().getColor(noSelectColorId));
            tvTwoOrder.setTextColor(getResources().getColor(noSelectColorId));
            tvThreeOrder.setTextColor(getResources().getColor(selectedColorId));
            tvFourOrder.setTextColor(getResources().getColor(noSelectColorId));
            tvFiveOrder.setTextColor(getResources().getColor(noSelectColorId));
            viewAllOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewTwoOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewThreeOrder.setBackgroundColor(getResources().getColor(selectedColorId));
            viewFourOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewFiveOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewAllOrder.setVisibility(INVISIBLE);
            viewTwoOrder.setVisibility(INVISIBLE);
            viewThreeOrder.setVisibility(VISIBLE);
            viewFourOrder.setVisibility(INVISIBLE);
            viewFiveOrder.setVisibility(INVISIBLE);
            if (viewPager!=null){
                viewPager.setCurrentItem(2);
            }
            if (threeTabClickListener!=null){
                threeTabClickListener.onThreeTabClick(v);
            }
        });
        
        rlnFourOrder.setOnClickListener(v -> {
            tvContentAll.setTextColor(getResources().getColor(noSelectColorId));
            tvTwoOrder.setTextColor(getResources().getColor(noSelectColorId));
            tvThreeOrder.setTextColor(getResources().getColor(noSelectColorId));
            tvFourOrder.setTextColor(getResources().getColor(selectedColorId));
            tvFiveOrder.setTextColor(getResources().getColor(noSelectColorId));
            viewAllOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewTwoOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewThreeOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewFourOrder.setBackgroundColor(getResources().getColor(selectedColorId));
            viewFiveOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewAllOrder.setVisibility(INVISIBLE);
            viewTwoOrder.setVisibility(INVISIBLE);
            viewThreeOrder.setVisibility(INVISIBLE);
            viewFourOrder.setVisibility(VISIBLE);
            viewFiveOrder.setVisibility(INVISIBLE);
            if (viewPager!=null){
                viewPager.setCurrentItem(3);
            }
            if (fourTabClickListener!=null){
                fourTabClickListener.onFourTabClick(v);
            }
        });
        
        rlnFiveOrder.setOnClickListener(v -> {
            tvContentAll.setTextColor(getResources().getColor(noSelectColorId));
            tvTwoOrder.setTextColor(getResources().getColor(noSelectColorId));
            tvThreeOrder.setTextColor(getResources().getColor(noSelectColorId));
            tvFourOrder.setTextColor(getResources().getColor(noSelectColorId));
            tvFiveOrder.setTextColor(getResources().getColor(selectedColorId));
            viewAllOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewTwoOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewThreeOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewFourOrder.setBackgroundColor(getResources().getColor(noSelectColorId));
            viewFiveOrder.setBackgroundColor(getResources().getColor(selectedColorId));
            viewAllOrder.setVisibility(INVISIBLE);
            viewTwoOrder.setVisibility(INVISIBLE);
            viewThreeOrder.setVisibility(INVISIBLE);
            viewFourOrder.setVisibility(INVISIBLE);
            viewFiveOrder.setVisibility(VISIBLE);
            if (viewPager!=null){
                viewPager.setCurrentItem(4);
            }
            if (fiveTabClickListener!=null){
                fiveTabClickListener.onFiveTabClick(v);
            }
        });
    }

    public void setViewPager(ViewPager viewPager){
        this.viewPager = viewPager;
    }

    public void setSelectedColorId(int selectedColorId) {
        this.selectedColorId = selectedColorId;
    }

    public void setnoSelectColorId(int noSelectColorId) {
        this.noSelectColorId = noSelectColorId;
    }

    public void setOneTabClickListener(OneTabClickListener oneTabClickListener) {
        this.oneTabClickListener = oneTabClickListener;
    }

    public void setTwoTabClickListener(TwoTabClickListener twoTabClickListener) {
        this.twoTabClickListener = twoTabClickListener;
    }

    public void setThreeTabClickListener(ThreeTabClickListener threeTabClickListener) {
        this.threeTabClickListener = threeTabClickListener;
    }

    public void setFourTabClickListener(FourTabClickListener fourTabClickListener) {
        this.fourTabClickListener = fourTabClickListener;
    }

    public void setFiveTabClickListener(FiveTabClickListener fiveTabClickListener) {
        this.fiveTabClickListener = fiveTabClickListener;
    }

    public interface OneTabClickListener{
        void onOneTabClick(View view);
    }

    public interface TwoTabClickListener{
        void onTwoTabClick(View view);
    }

    public interface ThreeTabClickListener{
        void onThreeTabClick(View view);
    }

    public interface FourTabClickListener{
        void onFourTabClick(View view);
    }

    public interface FiveTabClickListener{
        void onFiveTabClick(View view);
    }

}
